//-----------------------------------------------------------------------------
// sphere
//-----------------------------------------------------------------------------


#include <string.h>
#include <stdio.h>
#include "../Config.h"
#include "../Vetor3D.h"
#include "Sphere.h"
#include <math.h>
//#include "../Terrain.h"

//-----------------------------------------------------------------------------
// constructor
//-----------------------------------------------------------------------------
CSphere::CSphere() {

  memset(m_afPosition, 0, sizeof(float) * 3);
  m_fRadius = 0.0f;
}


//-----------------------------------------------------------------------------
// destructor
//-----------------------------------------------------------------------------
CSphere::~CSphere() {
}


//-----------------------------------------------------------------------------
// initialize the sphere
//-----------------------------------------------------------------------------
bool CSphere::Init(float *pfPosition, float fRadius) {

  memcpy(m_afPosition, pfPosition, sizeof(float) * 3);
  m_fRadius = fRadius;

  return true;
}


//-----------------------------------------------------------------------------
// draw
//-----------------------------------------------------------------------------
void CSphere::Draw() {

  glPushMatrix();
  glTranslatef(m_afPosition[0], m_afPosition[1], m_afPosition[2]);
  glDisable(GL_LIGHTING);
  glEnable(GL_BLEND);
  glColor4f(1.0f, 0.0f, 0.0f, 0.5f);
  glutSolidSphere(m_fRadius, 10, 10);
  glDisable(GL_BLEND);
  glEnable(GL_LIGHTING);
  glPopMatrix();
}

//-----------------------------------------------------------------------------
// chech collision
//-----------------------------------------------------------------------------
float CSphere::CheckCollisionRay(float *pfPosition, float *pfDirection) {

    float afPercurso[3];
    float distancia;
    float x,y,z;
    afPercurso[0] = pfDirection[0];
    afPercurso[1] = pfDirection[1];
    afPercurso[2] = pfDirection[2];
    int percursoIdx;

    //arrumar para pegar de outro lugar tais dados
    int m_nWidth = 128;
    int m_nDepth = 128;
    
    distancia = -1.0f;

        //getchar(); + scanf("c\n");
        if ((int)afPercurso[0] == (int)m_afPosition[0] && (int)afPercurso[1] == (int)m_afPosition[1] && (int)afPercurso[2] == (int)m_afPosition[2]) {
            printf ("HIT Al: %f %f %f > %f %f %f\n",afPercurso[0],afPercurso[1],afPercurso[2],m_afPosition[0],m_afPosition[1],m_afPosition[2]);
            x = pfPosition[0] - m_afPosition[0];
            y = pfPosition[1] - m_afPosition[1];
            z = pfPosition[2] - m_afPosition[2];
            //distancia entre dois pontos 3d
            distancia = sqrt(pow(x,2)+pow(y,2)+pow(z,2));
       }

    return distancia;
}
